<?php 
namespace Home\Controller;
use Think\Controller;
/**
* 发布问题
*/
class PublishController extends CommonController
{	
	//发布问题
	public function index()
	{  
        if ($_SESSION['id']) {
            $id=I('get.id');
            if ($id) {
              $src = M('question')->where("id='$id'")->select();
              $src=current($src);
              $this->assign('arr',$src);
            }
            //验证码
            $question= D('vercode')->getOne();
            $this->assign('question',$question);
            // 问题分类
            $class=M('category')->select();
            // var_dump($class);
            $class=$this->staTree($class);
            // var_dump($class);
            // exit;
            $this->assign('class',$class);
            $this->display();
        }else{
            $_SESSION['wc']=0;
            header("Location:".U('home/enter/index'));
        }
        
	}
	// 图片下载
	public function upload()
	{
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/pic/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
        	$arr=array(
        		"code"=>'1' ,//0表示成功，其它失败,
        		"msg"=>$upload->getError()//提示信息 //一般上传失败后返回
        		);
        	echo json_encode($arr);
        }else{// 上传成功
        	$info = current($info);
        	$arr=array(
        		"code"=>'0',
        		'msg'=>'上传成功',
        		'data'=>array(
        			"src"=>__ROOT__."/Uploads/pic/".$info['savepath'].$info['savename'],
        			"title"=> "图片"
        			)
        		);
           echo json_encode($arr);
        }
    }
    // 问题发布验证
    public function add()
    {	
    	// 人类验证
    	$this->checkVercode();
    	$data=I('post.');
    	$cid=M('category')->where("cname='$data[class]'")->select();
    	$data['cid']=$cid[0]['id'];
    	$data['uid']=$_SESSION['id'];
    	$data['create_time']=time();
    	// 写入数据库
    	$rec=M('question')->add($data);
    	// echo json_encode($rec);
    	// exit;
    	if($rec){
    		$url = U('home/publish/issue',array('id'=>$rec));
    		$arr = array(
             'error'=>'0',
             'info'=>'发布成功',
             'url'=>$url,
             );
    		echo json_encode($arr);
    	}else{
    		$arr = array(
             'error'=>'1',
             'info'=>'发布失败',
             );
    		echo json_encode($arr);
    	}
    	
    }
    // 问题页面
    public function issue()
    {	
        // 主要问题展现
    	$id=I('get.id');
    	$this->inc('question','view_num',$id,1);
    	$sql ="select t1.*,t2.nikename,t2.face from question t1 left join user t2 on t1.uid=t2.id where t1.id='$id'";
    	$arr = M()->query($sql);
    	$arr = current($arr);
        // 回答展现
    	$sql ="select t1.*,t2.nikename,t2.face from answer t1 left join user t2 on t1.uid=t2.id where t1.qid='{$id}'";
    	
    	$data = M()->query($sql);
    	for ($i=0; $i <count($data) ; $i++) { 
    		$data[$i]['create_time']=getFormate($data[$i]['create_time']);
    	}
        foreach ($data as &$v) {
            $sql = "select id from zan where aid='{$v['id']}' and uid='{$_SESSION['id']}'";
            $v['stt']=M()->query($sql);
            $v['stt']=current($v['stt']);
            // var_dump($v['stt']);
        }
        // var_dump($arr);
        // var_dump($data);
        // exit;
        //旁边问题排行
        $time = strtotime('-30 days');
        $list = M('question')->where("create_time>'{$time}'")->order('view_num desc')->limit(10)->select();
        $ans = M('question')->where("create_time>'{$time}'")->order('answer_num desc')->limit(10)->select();

    	$arr['create_time'] = getFormate($arr['create_time']);
        
    	$this->assign('arr',$arr);
    	$this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('ans',$list);
    	// var_dump($data);
    	$this->display();
    }
    // 答案展现
    public function answer()
    {
        if (!$_SESSION['id']) {
            $arr = array(
             'error'=>'1',
             'info'=>'请登录后再发表评论',
            );
            echo json_encode($arr);
            exit;
        }
    	$data=I('post.');
    	$data['uid'] = $_SESSION['id'];
    	$data['create_time']=time();
    	
       	$rec=M('answer')->add($data);
    	if ($rec) {
    		$sql ="select t1.*,t2.nikename,t2.face from answer t1 left join user t2 on t1.uid=t2.id where t1.id='$rec'";
    		$data = M()->query($sql);
    		$data = current($data);
    		$data['content'] = htmlspecialchars_decode($data['content']);
    		$data['create_time']=getFormate($data['create_time']);
    		$data['error']='0';
    		$data['info']='发布成功';
            $this->inc('question','answer_num',$data['qid'],1);
    		echo json_encode($data);
    	}else{
    		$arr = array(
             'error'=>'1',
             'info'=>'发布失败',
            );
    		echo json_encode($arr);
    		exit;
    	}
    }
    //点赞
    public function zan()
    {
        $id=I('post.data');
        // $id=70;
        // echo json_encode(123); 
        // exit;
        $sql = "select id from zan where aid='{$id}' and uid='{$_SESSION['id']}'";
        $src = M()->query($sql);
        $src=current($src);
        if ($src) {
            $sql ="Update answer set zan=zan-1 where id='{$id}'";
            M()->execute($sql);  
            $sql ="delete from zan where id='{$src['id']}'";
            $cc = M()->execute($sql);
        }else{
          $sql ="Update answer set zan=zan+1 where id='{$id}'";
          M()->execute($sql); 
          $sre=M('zan')->add(array('aid'=>$id,'uid'=>"{$_SESSION['id']}"));
        }
       $zan= M('answer')->field('zan')->where("id='{$id}'")->select();
        // M('zan')->where("aid=")->select('')
        // $str = M('answer')->where("id={'$id'}")->save(array('zan'=>''))
       $zan = current($zan);
        // var_dump($zan);
        echo json_encode($zan); 
    }
    //采纳
    public function cn()
    {
        $id=I('post.');
        $this->inc('question','cn',$id['qid'],1);
        $this->inc('answer','status',$id['aid'],1);
        echo json_encode(1);
        // var_dump($id);
    }
}



 ?>